export function debounce(fn,delay = 500,isImmediate = false,dataCallBack) {
  let timer = null 
  let isInvoke = false
  function _debounce(...args) {
    return new Promise((resolve) => {
      if(timer) clearTimeout(timer)
      if(isImmediate && !isInvoke){
        const res = fn.apply(this,args)
        if(dataCallBack) dataCallBack(res)
        resolve(res)
        isInvoke = true
      }else{
        timer = setTimeout(() => {
          const res = fn.apply(this,args)
          if(dataCallBack) dataCallBack(res)
          resolve(res)
          isInvoke = false
          timer = null
        },delay)
      }
    })
   
  }
  _debounce.cancel = () =>  {
    if(timer) clearTimeout(timer)
    timer = null
    isInvoke = false
  }
  return _debounce
}